var Config = require("../config");
var KOC = require("koc.common.utils");
var Geetest = new (require("geetest"))({
    geetest_id: Config.Interface.Geetest.ID,
    geetest_key: Config.Interface.Geetest.Key
});

module.exports = {
    Register: function (onComplete) {
        Geetest.register(function (data) {
            var retValue = KOC.ReturnValue.New();
            if (data.success !== 1) {
                retValue.hasError = true;
                retValue.message = "获取验证码失败";
            } else {
                retValue.returnObject = {
                    ID: Geetest.geetest_id,
                    Challenge: data.challenge
                };
            }
            if (typeof onComplete === 'function') {
                onComplete(retValue);
            }
        });
    },
    Validate: function (parm, onComplete) {
        if (!parm || !parm.challenge || !parm.validate || !parm.seccode) {
            if (typeof onComplete === 'function') {
                onComplete(KOC.ReturnValue.New({
                    hasError: true,
                    message: "验证码验证失败,Param Error!"
                }));
            }
            return;
        }
        Geetest.validate(parm, function (err, result) {
            var retValue = KOC.ReturnValue.New();
            if (err || !result) {
                retValue.hasError = true;
                retValue.message = "验证码验证失败";
            }
            if (typeof onComplete === 'function') {
                onComplete(retValue);
            }
        });
    }
};